package com.xuxueli.commontdemo.commont;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;

/**
 * 系统日志，切面处理类，记录日志
 */
@Aspect
@Component
public class SysLogAspect {

//	@Autowired
//	private SysLogService sysLogService;
//
//	@Pointcut("execution(* com.test.service.*.*(..))")
//	public void logPointCut() {
//
//	}
//
//	@Around("logPointCut()")
//	public Object around(ProceedingJoinPoint point) throws Throwable {
//		long beginTime = System.currentTimeMillis();
//		// 执行方法
//		Object result = point.proceed();
//		// 执行时长(毫秒)
//		long time = System.currentTimeMillis() - beginTime;
//		// 保存日志
//		saveSysLog(point, time);
//		return result;
//	}
//
//	private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
//		String userName = "admin";
//		if(joinPoint.getTarget() instanceof SysLogService) {
//			return ;
//		}
//		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//		SysLog sysLog = new SysLog();
//
////		Method method = signature.getMethod();
////		com.louis.merak.admin.annotation.SysLog syslogAnno = method.getAnnotation(com.louis.merak.admin.annotation.SysLog.class);
////		if(syslogAnno != null){
////			//注解上的描述
////			sysLog.setOperation(syslogAnno.value());
////		}
//
//		// 请求的方法名
//		String className = joinPoint.getTarget().getClass().getName();
//		String methodName = signature.getName();
//		sysLog.setMethod(className + "." + methodName + "()");
//
//		// 请求的参数
//		Object[] args = joinPoint.getArgs();
//		try{
//			String params = JSONObject.toJSONString(args[0]);
//			if(params.length() > 200) {
//				params = params.substring(0, 200) + "...";
//			}
//			sysLog.setParams(params);
//		} catch (Exception e){
//		}
//
//		// 获取request
//		HttpServletRequest request = HttpUtils.getHttpServletRequest();
//		// 设置IP地址
//		sysLog.setIp(IPUtils.getIpAddr(request));
//
//		// 用户名
//		sysLog.setUserName(userName);
//
//		// 执行时长(毫秒)
//		sysLog.setTime(time);
//
//		// 保存系统日志
//		sysLogService.insert(sysLog);
//	}


    @Pointcut("execution(public * com.xuxueli.commontdemo.controller.*.*(..))")
    public void pointcut() { }

    @Before("pointcut()")
    public void  doBefore(JoinPoint joinPoint) {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        assert sra != null;
        HttpServletRequest request = sra.getRequest();
        String dbtype = request.getHeader("dbtype");
        //给当前的执行SQL的操作设置特殊的数据源的信息
        if(StringUtils.isNotBlank(dbtype)){
            DataSourceContextHolders.setDB("slaveDataSource");
        }
    }

    @After("pointcut()")
    public void after(JoinPoint point) {
        //清理掉当前设置的数据源，让默认的数据源不受影响
        DataSourceContextHolders.clearDB();
    }
}